<?php
class subscribe_newsclass {

	function subscribe_newsclass() {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}

	function add_new_registation($email){
		if ((isset($email)) && (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $email))){
			$email = trim($email);
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."subscribe_news` WHERE `email`='".$email."'", __LINE__, __FILE__);
			if (mysql_num_rows($sql) == 0) $sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."subscribe_news` (`id`,`email`,`status`,`regdate`,`unregdata`) VALUES ('', '".$email."', '1', '".date('YmdHis')."', '".date('YmdHis')."')", __LINE__, __FILE__);
			else return 14;
		} else return 13;
		return 1;
	}

	function emails_del($emails_array){
		foreach($emails_array as $elem){
			if (isset($elem) and ($elem > 0)){
				$sql = $this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."subscribe_news` WHERE `id` = ".$elem, __LINE__, __FILE__);		
			}
		}
	}
	
	function get_subscribers_by_search($search_query){
		$result = array();
		if (isset($search_query) && (strlen($search_query) > 0)){
			$search_query = trim($search_query);
			$query = "SELECT * FROM `".$this->setupdata['table_prefix']."subscribe_news` WHERE `email` LIKE '%".$search_query."%'";
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $result[$row['id']] = $row;
		}
		return $result;
	}
	
	function remove_registation($email){
		if ((isset($email)) && (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $email))){
			$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."subscribe_news` SET `status`=0, `unregdata`='".date('YmdHis')."' WHERE `email` = '".$email."'", __LINE__, __FILE__);		
		}
	}
	
	function emails_export(){
		$result = "";
		$sql = $this->mysqlclass->mysql_my_query("SELECT `email` FROM `".$this->setupdata['table_prefix']."subscribe_news` WHERE `status`=1 ORDER BY `regdate` DESC", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $result .= $row['email']."\n";
		header("Content-type: text/plain");
		header('Content-Disposition: attachment; filename="subscribers_'.date('H_i_s').'.csv"');
		header('Content-length: '.strlen($result));
		echo($result);
		exit();
	}

	function emails_export_unsubscribers(){
		$result = "";
		$sql = $this->mysqlclass->mysql_my_query("SELECT `email` FROM `".$this->setupdata['table_prefix']."subscribe_news` WHERE `status`=0 ORDER BY `regdate` DESC", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $result .= $row['email']."\n";
		header("Content-type: text/plain");
		header('Content-Disposition: attachment; filename="unsubscribers_'.date('H_i_s').'.csv"');
		header('Content-length: '.strlen($result));
		echo($result);
		exit();
	}

}
?>
